﻿using KeyboardBubble.Common;
using KeyboardBubble.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KeyboardBubble
{
    public class HotkeyManager
    {
        private Dictionary<string, Hotkey> hotkeys = new Dictionary<string, Hotkey>();

        public void RegisterHotkey(string keyname)
        {
            // 读取配置文件
            // 配置文件行说明：
            // 1  配置名称
            // 2  配置解释
            // 3  是否锁定：如值为 =lock= 则为锁定文件，不允许修改
            // 4-n  具体快捷键配置
            var lines = FileHelp.ReadKeymapFile(keyname);

            for (var i = 3; i < lines.Count; i++)
            {
                try
                {
                    var s1 = lines[i].Split(new char[] { ':' });
                    // 字符解析
                    var k1 = s1[0].Split(new char[] { '+' });
                    if (k1.Length == 1)
                    {
                        // 组合键字符串文本和键值
                        string keyOneText = k1[0].FirstUpper();
                        int keyOne = Common.KeyboardKeys.Instace.FirstOrDefault(x => x.Value == keyOneText).Key;

                        hotkeys[keyOne.ToString()] = new Hotkey(keyOne, null, null, keyOneText, s1[1]);
                    }
                    else if (k1.Length == 2)
                    {
                        // 组合键字符串文本和键值
                        string keyOneText = k1[0].FirstUpper();
                        int keyOne = Common.KeyboardKeys.Instace.FirstOrDefault(x => x.Value == keyOneText).Key;
                        string keyTwoText = k1[1].FirstUpper();
                        int keyTwo = Common.KeyboardKeys.Instace.FirstOrDefault(x => x.Value == keyTwoText).Key;
                        string keyString = keyOne + "+" + keyTwo;
                        string keyNote = keyOneText + "+" + keyTwoText;

                        hotkeys[keyString] = new Hotkey(keyOne, keyTwo, null, keyOneText, s1[1]);
                    }
                    else if (k1.Length == 3)
                    {
                        // 组合键字符串文本和键值
                        string keyOneText = k1[0].FirstUpper();
                        int keyOne = Common.KeyboardKeys.Instace.FirstOrDefault(x => x.Value == keyOneText).Key;
                        string keyTwoText = k1[1].FirstUpper();
                        int keyTwo = Common.KeyboardKeys.Instace.FirstOrDefault(x => x.Value == keyTwoText).Key;
                        string keyThreeText = k1[2].FirstUpper();
                        int keyThree = Common.KeyboardKeys.Instace.FirstOrDefault(x => x.Value == keyThreeText).Key;
                        string keyString = keyOne + "+" + keyTwo + "+" + keyThree;
                        string keyNote = keyOneText + "+" + keyTwoText + "+" + keyThreeText;

                        hotkeys[keyString] = new Hotkey(keyOne, keyTwo, keyThree, keyOneText, s1[1]);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("配置文件读取错误：" + e.Message);
                }
            }
        }

        public bool CheckHotkeys(HashSet<int> keys)
        {
            List<int> list = new List<int>(keys);
            if (keys.Count == 1) // 如果只匹配一个键
            {
                Dictionary<string, Hotkey> one = hotkeys.Where(x => !x.Value.KeyThree.HasValue && !x.Value.KeyTwo.HasValue)
                    .ToDictionary(x => x.Key, x => x.Value);
                var key = one.FirstOrDefault(x => x.Value.KeyOne == list.First());
                if (key.Value != null)
                {
                    key.Value.ShowBox();
                    return true;
                }
            }
            if (keys.Count == 2)
            {
                Dictionary<string, Hotkey> two = hotkeys.Where(x => !x.Value.KeyThree.HasValue)
                    .ToDictionary(x => x.Key, x => x.Value);
                var key = two.FirstOrDefault(x => (x.Value.KeyOne == list.First() || x.Value.KeyTwo == list.First())
                && (x.Value.KeyOne == list[1] || x.Value.KeyTwo == list[1]));
                if (key.Value != null)
                {
                    key.Value.ShowBox();
                    return true;
                }
            }
            //foreach (var hotkey in hotkeys.Values)
            //{
            //    if (e == hotkey.KeyCode && e == hotkey.Modifiers)
            //    {
            //        hotkey.Action();
            //    }
            //}
            return false;
        }
    }
}
